package jude.java.money.batch.bean;

import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;

import jude.java.util.DateUtils;

/** 网站空间 */
public class Space {

	private String name;
	private String url;
	private String user;
	private String pass;
	private Date startDate;
	private Date endDate;
	private String dbuser;
	private String dbpass;
	private String dns1;
	private String dns2;
	private String email;
	private String apass;
	private int index;
	private String remark;
	private String[] blogs;
	private String[] domains;
	private String[] parked;		//曾经依靠的域名
	private String limit;			//空间限制: 附加域, 停靠域, 硬盘空间, 流量 
	
	public String getName() {
		return name;
	}
	public Space setName(String name) {
		this.name = name;
		return this;
	}
	public String getUrl() {
		return url;
	}
	public Space setUrl(String ip) {
		this.url = ip;
		return this;
	}
	public String getUser() {
		return user;
	}
	public Space setUser(String user) {
		this.user = user;
		return this;
	}
	public String getPass() {
		return pass;
	}
	public Space setPass(String pass) {
		this.pass = pass;
		return this;
	}
	public Date getStartDate() {
		return startDate;
	}
	public Space setStartDate(Date start) {
		this.startDate = start;
		return this;
	}
	/** yyyy-M-d */
	public Space setStartDate(String start) {
		this.startDate = DateUtils.toDate(start, DateUtils.DEFAULT_DATE_FORMAT);
		return this;
	}
	public Date getEndDate() {
		return endDate;
	}
	public Space setEndDate(Date end) {
		this.endDate = end;
		return this;
	}
	/** yyyy-M-d */
	public Space setEndDate(String end) {
		this.endDate = DateUtils.toDate(end, DateUtils.DEFAULT_DATE_FORMAT);
		return this;
	}
	public String getDbuser() {
		return dbuser;
	}
	public Space setDbuser(String dbuser) {
		this.dbuser = dbuser;
		return this;
	}
	public String getDbpass() {
		return dbpass;
	}
	public Space setDbpass(String dbpass) {
		this.dbpass = dbpass;
		return this;
	}
	public String getDns1() {
		return dns1;
	}
	public Space setDns1(String dns1) {
		this.dns1 = dns1;
		return this;
	}
	public String getDns2() {
		return dns2;
	}
	public Space setDns2(String dns2) {
		this.dns2 = dns2;
		return this;
	}
	public String getEmail() {
		return email;
	}
	public Space setEmail(String email) {
		this.email = email;
		return this;
	}
	public int getIndex() {
		return index;
	}
	public Space setIndex(int index) {
		this.index = index;
		return this;
	}
	public String getRemark() {
		return remark;
	}
	public Space setRemark(String remark) {
		this.remark = remark;
		return this;
	}
	public String[] getDomains() {
		return domains;
	}
	public Space setDomains(String[] domains) {
		this.domains = domains;
		return this;
	}
	public String[] getParked() {
		return parked;
	}
	public Space setParked(String... parked) {
		this.parked = parked;
		return this;
	}
	public String[] getBlogs() {
		return blogs;
	}
	public Space setBlogs(String[] domains) {
		Arrays.sort(domains, COMPARATOR);
		this.blogs = domains;
		return this;
	}
	public String getApass() {
		return apass;
	}
	public Space setApass(String apass) {
		this.apass = apass;
		return this;
	}
	/** 获取空间限制 */
	public String getLimit() {
		return limit;
	}
	public Space setLimit(int addon, int parked, String space, String bw) {
		if (addon <= 0 && parked <= 0 && space == null && bw == null) {
			this.limit = "∞";
			return this;
		}
		StringBuilder sb = new StringBuilder();
		if (addon >= 0)
			sb.append("A:").append(addon).append(" ");
		if (parked >= 0)
			sb.append("P:").append(parked).append(" ");
		if (space != null)
			sb.append("Space:").append(space).append(" ");
		if (bw != null)
			sb.append("BW:").append(bw);
		this.limit = sb.toString();
		return this;
	}
	private static final Comparator<String> COMPARATOR = new Comparator<String>() {
		@Override
		public int compare(String o1, String o2) {
			boolean tk1 = o1.endsWith(".tk");
			boolean tk2 = o2.endsWith(".tk");
			if (tk1 && !tk2)
				return 1;
			if (!tk1 && tk2)
				return -1;
			if (tk1 && tk2 || !tk1 && tk2)
				return o1.compareTo(o2);
			return 0;
		}
	};
}
